package com.pyue.eam.regi.module.region.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyue.eam.admin.api.entity.eam.EamRegion;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.regi.module.region.mapper.EamRegionMapper;
import com.pyue.eam.regi.module.region.service.RegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl extends ServiceImpl<EamRegionMapper, EamRegion> implements RegionService {
    @Override
    public List<Tree<String>> cityData(String name, String parentId) {

        final List<EamRegion> nodeList = baseMapper.selectList(Wrappers.lambdaQuery(EamRegion.class).
                in(EamRegion::getLayer, CollUtil.newArrayList(1, 2))
                .like(StrUtil.isNotBlank(name), EamRegion::getName, name)
                .eq(StrUtil.isNotBlank(parentId), EamRegion::getPCode, parentId)
        );

        if (CollUtil.isEmpty(nodeList)) {
            return CollUtil.newArrayList();
        }

        List<TreeNode<String>> treeNodeList = nodeList.stream()
                .filter(area -> !area.getCode().equals(area.getPCode()))
                .map(area -> {
                    TreeNode<String> node = new TreeNode<>();
                    node.setId(area.getCode());
                    node.setParentId(area.getPCode());
                    node.setWeight(area.getCode());

                    // 扩展属性
                    Map<String, Object> extra = new HashMap<>();
                    extra.put("hourMin", area.getHourMin());
                    extra.put("hourMax", area.getHourMax());
                    extra.put("dayMin", area.getDayMin());
                    extra.put("dayMax", area.getDayMax());
                    extra.put("createByName", area.getCreateByName());
                    extra.put("createTime", area.getCreateTime());

                    node.setExtra(extra);
                    node.setName(area.getName());
                    return node;
                }).collect(Collectors.toList());

        if (StrUtil.isNotBlank(name)) {
            // 模糊查询 不组装树结构 直接返回 表格方便编辑
            return treeNodeList.stream().map(node -> {
                Tree<String> tree = new Tree<>();
                tree.putAll(node.getExtra());
                BeanUtils.copyProperties(node, tree);
                return tree;
            }).collect(Collectors.toList());
        } else {
            return TreeUtil.build(treeNodeList, parentId != null ? parentId : "0");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCityPrice(EamRegion eamRegion) {
        ApiAsserts.isTrue(eamRegion.getDayMax().compareTo(eamRegion.getDayMin()) > 0, "价格区间不合法");
        ApiAsserts.isTrue(eamRegion.getHourMax().compareTo(eamRegion.getHourMin()) > 0, "价格区间不合法");

        // todo 如果下级没价格已当前价格
        EamRegion db = baseMapper.selectById(eamRegion.getId());
        ApiAsserts.isTrue(db != null, "城市数据不存在");
        db.setDayMin(eamRegion.getDayMin());
        db.setDayMax(eamRegion.getDayMax());

        db.setHourMin(eamRegion.getHourMin());
        db.setHourMax(eamRegion.getHourMax());
        baseMapper.updateById(db);
        return Boolean.TRUE;
    }
}
